package com.youlai.system.plugin.easyexcel;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.youlai.system.converter.EmployConverter;
import com.youlai.system.model.entity.Employ;
import com.youlai.system.model.vo.EmployImportVO;
import com.youlai.system.service.DepartmentService;
import com.youlai.system.service.EmployService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.extra.spring.SpringUtil;

@Slf4j
@Data
public class EmployImportListener extends MyAnalysisEventListener<EmployImportVO>{

    // 有效条数
    private int validCount;

    // 无效条数
    private int invalidCount;

    // 导入返回信息
    StringBuilder msg = new StringBuilder();

    private final EmployService employService;
    private final EmployConverter employConverter;
    private final DepartmentService departmentService;

    public EmployImportListener(){
        this.employService = SpringUtil.getBean(EmployService.class);
        this.employConverter = SpringUtil.getBean(EmployConverter.class);
        this.departmentService = SpringUtil.getBean(DepartmentService.class);

    }
    // 部门ID

    @Override
    public String getMsg() {
        // 总结信息
        String summaryMsg = StrUtil.format("导入员工结束：成功{}条，失败{}条；<br/>{}", validCount, invalidCount, msg);
        return summaryMsg;
    }

    @Override
    public void invoke(EmployImportVO employImportVO, AnalysisContext context) {
        log.info("解析到一条用户数据:{}", JSONUtil.toJsonStr(employImportVO));
        //校验数据
        StringBuilder validationMsg = new StringBuilder();

        String username = employImportVO.getUsername();
        if (StrUtil.isBlank(username)){
            validationMsg.append("用户名为空：");
        }else {
            long count = employService.count(new LambdaUpdateWrapper<Employ>().eq(Employ::getUsername,username));
            if (count>0){
                validationMsg.append("员工已存在");
            }
        }

        String mobile = employImportVO.getMobile();
        if (StrUtil.isBlank(mobile)){
            validationMsg.append("手机号码为空：");
        }else {
            if (!Validator.isMobile(mobile)){
                validationMsg.append("手机号码不正确：");
            }
        }
        String formOfEmployment = employImportVO.getFormOfEmployment();
        if (formOfEmployment.equals("正式")){
            employImportVO.setFormOfEmployment(formOfEmployment= String.valueOf(1));
        }else {
            employImportVO.setFormOfEmployment(formOfEmployment= String.valueOf(2));
        }

        if (validationMsg.isEmpty()){
            //校验通过，持久化至数据库
            Employ employ = employConverter.importVo2Entity(employImportVO);

            Integer departmentId = departmentService.selectByDepName(employImportVO.getDepartmentName());
//            String formOfEmployment = employImportVO.getFormOfEmployment();

            employ.setDepartmentId(departmentId);
            employ.setFormOfEmployment(Integer.parseInt(formOfEmployment));

            boolean saveResult  = employService.insertEmploy(employ);
            if (saveResult){
                validCount++;

            }else {
                invalidCount++;
                msg.append("第").append(validCount + invalidCount).append("行数据保存失败；<br/>");
            }
        }else {
            invalidCount++;
            msg.append("第" + (validCount + invalidCount) + "行数据校验失败：").append(validationMsg + "<br/>");
        }



    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
}
